﻿using System.Collections.Generic;

namespace Whoot
{
    public class SequenceList : TaskList
    {
        private int _curIndex;

        protected override void OnExecute()
        {
            _curIndex = 0;
            Progress = 0;
        }

        protected override void OnUpdate()
        {
            if (Tasks.Count == 0)
            {
                EndTask();
                return;
            }

            CheckInSequenceTask();
        }


        private void CheckInSequenceTask()
        {
            for (var i = _curIndex; i < Tasks.Count; i++)
            {
                var status = Tasks[i].Tick();

                switch (status)
                {
                    case TaskStatus.Failure:
                        EndTask(false);
                        Tasks[i].OnFinished?.Invoke(false);
                        return;
                    case TaskStatus.Running:
                        _curIndex = i;
                        return;
                    default:
                        Tasks[i].OnFinished?.Invoke(true);
                        break;
                }
            }

            EndTask();
        }

        protected override void OnReset()
        {
            foreach (var a in Tasks)
            {
                a.Reset();
            }

            _curIndex = 0;
        }
        
    }
}